<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.user.inc.php');
require_once(__PHY_LIB.'/class/uEnroll.class.php');
require_once(__PHY_LIB.'/class/uUser.class.php');

$rReq = validate($_REQUEST, 'action:e[new,add,upload]');
$rMsg = array();

if ('upload' == $rReq['action']) {
	do {
		if (!('user' == $_The['UserType'] && 0 < $_The['User']['id'])) { $rMsg['action'] .= 'please log in.'; break; }
		if (!($rP = validate($_GET, 'field:U'))) { $rMsg['action'] .= 'no field.'; break; }
		if (0 !== $_FILES[$rP['field']]['error']) { $rMsg['action'] .= 'no file.'; break; }
		$oUser = new uUser($_The['User']['id']);
		$sUrl = $oUser->calcThumbUrl(null, $rP['field']);
		mkdir_plus(dirname(__PHY_STO.$sUrl)); // 生成目录
		if (move_uploaded_file($_FILES[$rP['field']]['tmp_name'], __PHY_STO.$sUrl)) {
			$rMsg['action'] = $sUrl; // 保存成功
		}
		else { $rMsg['action'] .= 'failed.'; }
	} while (false);
	die($rMsg['action']);
}
elseif ('add' == $rReq['action']) {
	do {
		if (!('user' == $_The['UserType'] && 0 < $_The['User']['id'])) { $rMsg['action'] .= 'please log in.'; break; }
		$rP = validate($_POST, 'sign:u|contact:u|email:u|photo_url:u|demo:u|description:u|work1_url:u|work2_url:u|work3_url:u');
		$oTpl->assign('rP', $rP);
		if (!$rP['sign']) { $rMsg['sign'] .= '你还未填写。'; }
		if (!$rP['contact']) { $rMsg['contact'] .= '你还未填写。'; }
		if (!$rP['email']) { $rMsg['email'] .= '你还未填写。'; }
		if (0 < sizeof($rMsg)) { break; }
		$oEnroll = new uEnroll();
		$oEnroll->set('user_id', $_The['User']['id']);
		$oEnroll->create('user_id');
		foreach ($rP as $sField => $xValue) {
			$oEnroll->set($sField, $xValue);
		}
		$oEnroll->set('sign_en', $rP['sign']);
		$oEnroll->set('time_added', date('Y-m-d H:i:s'));
		$oEnroll->set('status', 0);
		if ($oEnroll->update()) {
			$rMsg['action'] = 'done.';
		}
		else { $rMsg['action'] .= 'failed.'; }
	} while (false);
}

if ('new' == $rReq['action']) {
	$oEnroll = new uEnroll();
	$oEnroll->cfgWhere("user_id='{$_The['User']['id']}'");
	$oEnroll->load();
	if (0 < $oEnroll->getID() && 0 < $oEnroll->get('status')) {
		$rMsg['action'] .= 'Your enrollment requirement has been send.';
	}
}

$oTpl->assign('rMsg', $rMsg);
$oTpl->assign('sAction', $rReq['action']);
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
